package com.android.mios.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Settings {

	// private static final String SETTINGS_NAME = "Config";
	private static final String PK_ACCESSORY = "PK_Accessory";
	private static final String HWKEY = "HWKey";

	private static final String LOCAL_SERIALNUMBER = "LocalSerialNumber";
	private static final String LOCAL_IP = "LocalIP";
	private static final String USERNAME = "Username";
	private static final String PASSWORD = "Password";
	private static final String REMEMBER_PASSWORD = "RememberPassword";
	private static final String AUTO_LOGIN = "AutoLogin";
	private static final String LAST_IP = "LastIP";
	private static final String LAST_ENGINE_CONNECT = "LAST_ENGINE_CONNECT";
	// private static final String LAST_PK_ACCESSPOINT = "LAST_PK_AccessPoint";
	private static final String NOT_WARN_ON_TV_REMOTE_CONNECTION = "NotWarnOnTVRemoteConnection";
	private static final String LAST_WIFI = "LastWifi";

	private String localSerailNumber;
	private String localIP;
	private String username;
	private String password;
	private boolean rememberPassword, autoLogin;
	private int pkAccessory;
	private String hwKey;
	private String lastIP;
	private boolean lastWifi;
	private String lastEngineConnect;// (L=Local, R=Remote, N=No engine)
	// private int lastPkAccessPoint;// (the last serial number we connected to)
	private boolean notWarnOnTVRemoteConnection;
	// private boolean SIMULATE = false;
	private static Settings _instance;
	private String LAST_POLLING_RESPONSE = "";
	private Context mContext;
	private SharedPreferences sharedPreferences;

	public static Settings getInstance(Context context) {

		if (_instance == null)
			_instance = new Settings(context);
		return _instance;

	}

	public static Settings getInstance() {

		return _instance;
	}

	private Settings(Context context) {

		this.mContext = context;

		initData();
	}

	private void initData() {

		TLog.d("Settings initData start...");
		sharedPreferences = mContext.getSharedPreferences(
				Constants.CONFIG_PREFERENCE_NAME, Context.MODE_PRIVATE);
		username = sharedPreferences.getString(USERNAME, "");// guestsmtlc
																// qqqqq111
		password = sharedPreferences.getString(PASSWORD, "");
		rememberPassword = sharedPreferences
				.getBoolean(REMEMBER_PASSWORD, true);
		autoLogin = sharedPreferences.getBoolean(AUTO_LOGIN, true);
		pkAccessory = sharedPreferences.getInt(PK_ACCESSORY, 0);
		hwKey = sharedPreferences.getString(HWKEY, "");
		lastIP = sharedPreferences.getString(LAST_IP, "");
		lastWifi = sharedPreferences.getBoolean(LAST_WIFI, false);
		lastEngineConnect = sharedPreferences
				.getString(LAST_ENGINE_CONNECT, "");
		localSerailNumber = sharedPreferences.getString(LOCAL_SERIALNUMBER, "");
		localIP = sharedPreferences.getString(LOCAL_IP, "");
		// lastPkAccessPoint = sharedPreferences.getInt(LAST_PK_ACCESSPOINT, 0);
		notWarnOnTVRemoteConnection = sharedPreferences.getBoolean(
				NOT_WARN_ON_TV_REMOTE_CONNECTION, false);
	}

	// public boolean sim(){
	// return this.SIMULATE;
	// }

	public void save() {
		TLog.d("Saving setting...");
		Editor editor = sharedPreferences.edit();
		editor.putString(USERNAME, username);
		editor.putString(PASSWORD, password);
		editor.putString(LOCAL_SERIALNUMBER, localSerailNumber);
		editor.putString(LOCAL_IP, localIP);
		editor.putBoolean(REMEMBER_PASSWORD, rememberPassword);
		editor.putBoolean(AUTO_LOGIN, autoLogin);
		editor.putInt(PK_ACCESSORY, pkAccessory);
		editor.putString(HWKEY, hwKey);
		editor.putString(LAST_IP, lastIP);
		editor.putBoolean(LAST_WIFI, lastWifi);
		editor.putString(LAST_ENGINE_CONNECT, lastEngineConnect);
		// editor.putInt(LAST_PK_ACCESSPOINT, lastPkAccessPoint);
		editor.putBoolean(NOT_WARN_ON_TV_REMOTE_CONNECTION,
				notWarnOnTVRemoteConnection);
		editor.commit();
	}

	public String getLocalSerailNumber() {
		return localSerailNumber;
	}

	public void setLocalSerailNumber(String localSerailNumber) {
		this.localSerailNumber = localSerailNumber;
	}

	public String getLocalIP() {
		return localIP;
	}

	public void setLocalIP(String localIP) {
		this.localIP = localIP;
	}

	public void setPollingResult(String res) {
		LAST_POLLING_RESPONSE = res;
	}

	public String getPollingResult() {
		return LAST_POLLING_RESPONSE;
	}

	public String getCurrentUsername() {
		int flag = sharedPreferences.getInt(Constants.LOCAL_OR_DISTANCE, 1);
		return flag == 1 ? username : "";
	}

	public String getDistanceUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isRememberPassword() {
		return rememberPassword;
	}

	public void setRememberPassword(boolean rememberPassword) {
		this.rememberPassword = rememberPassword;
	}

	public boolean getAutoLogin() {
		return autoLogin;
	}

	public void setAutoLogin(boolean autoLogin) {
		this.autoLogin = autoLogin;
	}

	public int getPkAccessory() {
		return pkAccessory;
	}

	public void setPkAccessory(int pkAccessory) {
		this.pkAccessory = pkAccessory;
	}

	public String getHwKey() {
		return hwKey;
	}

	public void setHwKey(String hwKey) {
		this.hwKey = hwKey;
	}

	public String getLastIP() {
		return lastIP;
	}

	public void setLastIP(String lastIP) {
		this.lastIP = lastIP;
	}

	public boolean isLastWifi() {
		return lastWifi;
	}

	public void setLastWifi(boolean lastWifi) {
		this.lastWifi = lastWifi;
	}

	public String getLastEngineConnect() {
		return lastEngineConnect;
	}

	public void setLastEngineConnect(String lastEngineConnect) {
		this.lastEngineConnect = lastEngineConnect;
	}

	// public int getLastPkAccessPoint() {
	// return lastPkAccessPoint;
	// }

	// public void setLastPkAccessPoint(int lastPkAccessPoint) {
	// this.lastPkAccessPoint = lastPkAccessPoint;
	// }

	public boolean isNotWarnOnTVRemoteConnection() {
		return notWarnOnTVRemoteConnection;
	}

	public void setNotWarnOnTVRemoteConnection() {
		notWarnOnTVRemoteConnection = true;
	}

	public SharedPreferences getSharedPreferences() {
		return sharedPreferences;
	}
}
